//
//  CityHomePageViewController.m
//  littleLight's Project-A
//
//  Created by dllo on 16/5/14.
//  Copyright © 2016年 light. All rights reserved.
//

#import "CityHomePageViewController.h"
#import <AFNetworking.h>
#import "CityPageModel.h"
#import "CityPageTableViewCell.h"
#import "UserViewController.h"
#import "BeijingViewController.h"
@interface CityHomePageViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchResultsUpdating>
@property (nonatomic, strong) UIButton *fenButton;
@property (nonatomic, strong) UIButton *riButton;
@property (nonatomic, strong) UIButton *paiButton;
@property (nonatomic, strong) UITableView *cityTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UISearchBar *mySearchBar;
//@property (nonatomic, strong) UISearchDisplayController *searchDC;
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) NSMutableArray *searchArray;
//刷新
@end

@implementation CityHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithRed:251 / 255.0  green:248 / 255.0 blue:241 / 255.0 alpha:1];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0 / 255.0 green:192 / 255.0 blue:203 / 255.0 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"城市猎人带你玩";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName: [UIFont systemFontOfSize:18],NSFontAttributeName: [UIFont boldSystemFontOfSize:12]}];
    
    self.searchArray = [NSMutableArray array];
    
    
    
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    //    设置开始搜索时背景显示与否 页面背景阴影效果,一般不要设置.
    self.searchController.dimsBackgroundDuringPresentation = NO;
    //  设置点击搜索隐藏导航栏;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y,  self.searchController.searchBar.frame.size.width, 40);
    //设置searchBar位置自适应
    [self.searchController.searchBar sizeToFit];

    
   
    

//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
//    headerView.backgroundColor = [UIColor whiteColor];
 //   [self.view addSubview:headerView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(beijingAction)];
    //创建Button
//    self.fenButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.fenButton.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width / 3, 40);
//    [self.fenButton setTitle:@"分类" forState:UIControlStateNormal];
//    [self.fenButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    [headerView addSubview:self.fenButton];
//    
//    
//    self.riButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.riButton.frame = CGRectMake( [UIScreen mainScreen].bounds.size.width / 3, 0, [UIScreen mainScreen].bounds.size.width / 3, 40);
//    [self.riButton setTitle:@"日期筛选" forState:UIControlStateNormal];
//    [self.riButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    [headerView addSubview:self.riButton];
//    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 3, 5 , 1, 30)];
//    label1.backgroundColor = [UIColor grayColor];
//    label1.alpha = 0.4;
//    [headerView addSubview:label1];
//    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width *2 / 3, 5 , 1, 30)];
//    label2.backgroundColor = [UIColor grayColor];
//    label2.alpha = 0.4;
//    [headerView addSubview:label2];
//    
//    
//    self.paiButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.paiButton.frame = CGRectMake( [UIScreen mainScreen].bounds.size.width * 2 / 3, 0, [UIScreen mainScreen].bounds.size.width / 3, 40);
//    
//    [self.paiButton setTitle:@"排序" forState:UIControlStateNormal];
//    [self.paiButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    
//    [headerView addSubview:self.paiButton];
    
    self.cityTableView  = [[UITableView alloc] initWithFrame:CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width - 20, self.view.frame.size.height - 64 - 49 - 40) style:UITableViewStylePlain];
    self.cityTableView.backgroundColor = [UIColor whiteColor];
    self.cityTableView.showsHorizontalScrollIndicator = NO;
    self.cityTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.cityTableView];
    self.cityTableView.delegate = self;
    self.cityTableView.dataSource = self;
    [self.cityTableView registerClass:[CityPageTableViewCell class] forCellReuseIdentifier:@"reuse"];
    //********将搜索加到tableView的头视图上
    self.cityTableView.tableHeaderView = self.searchController.searchBar;
    self.cityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
   
   [manager GET:@"http://api.breadtrip.com/hunter/products/v2/?city_name=%E5%A4%A7%E8%BF%9E&lat=38.88209423142037&lng=121.5397439384468&sign=b908c529402712d807a2e81b2acbd366&start=0" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
       
       
   } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       NSArray *array = [responseObject objectForKey:@"product_list"];
       for (NSDictionary *dic in array) {
           CityPageModel *model = [[CityPageModel alloc] init];
           [model setValuesForKeysWithDictionary:dic];
           [self.dataArray addObject:model];
           // NSLog(@"%@",model.tab_list);
       }
       [MBProgressHUD hideHUDForView:self.view animated:YES];
       [self.cityTableView reloadData];
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
       
   }];
    
}

- (void)beijingAction
{
    BeijingViewController *beiVC = [[BeijingViewController alloc] init];
    [self.navigationController pushViewController:beiVC animated:YES];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchController.active) {
        return  self.searchArray.count;
    }
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    if (self.searchController.active) {
        CityPageModel *model = [self.searchArray objectAtIndex:indexPath.row];
        cell.model = model;
    }
    
    else{
    CityPageModel *model = [self.dataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    }
    __block CityHomePageViewController *VC = self;
    cell.myBlock = ^(NSNumber *myId){
        UserViewController *userVC = [[UserViewController alloc] init];
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:userVC];
        userVC.myId = myId;
        [VC presentViewController:naVC animated:YES completion:^{
            
            
        }];
        
    
    
    };
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 5 * 3;
    
}


- (void)rightButtonAction
{
    
}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = [self.searchController.searchBar text];
   // NSLog(@"====%@",searchString);
    
    [self.searchArray removeAllObjects];
    for (CityPageModel *model in self.dataArray) {
        if ([model.title rangeOfString:searchString].location != NSNotFound) {
            [self.searchArray addObject:model];
            
        }
    }
   // NSLog(@"searchArray=====%@",self.searchArray);
    [self.cityTableView reloadData];
    
    
    
    
    
}


 //清楚搜索框 否则跳转下一页依然存在
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (self.searchController.active) {
        self.searchController.active = NO;
        [self.searchController.searchBar removeFromSuperview];
        
    }
}













- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
